//
//  NSTimer + tyBlock.m
//  tyFoundation
//
//  Created by qihongru on 14/10/31.
//  Copyright (c) 2014年 ND. All rights reserved.
//

#import "NSTimer+TYBlock.h"

@interface tyTimerBlockHandler : NSObject
@property (nonatomic, copy) HandleTimerBlock block;
- (void)ty_handleBlockWithTimer:(NSTimer *)timer;
@end

@implementation NSTimer (TYBlock)

+ (NSTimer *)ty_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(HandleTimerBlock)block repeats:(BOOL)inRepeats{
    
    tyTimerBlockHandler *blockHandler = [[tyTimerBlockHandler alloc] init];
    blockHandler.block = block;
    
    return [self scheduledTimerWithTimeInterval:seconds target:blockHandler selector:@selector(ty_handleBlockWithTimer:) userInfo:nil repeats:inRepeats];
}

+ (NSTimer *)ty_timerWithTimeInterval:(NSTimeInterval)seconds block:(HandleTimerBlock)block repeats:(BOOL)inRepeats{
    
    tyTimerBlockHandler *blockHandler = [[tyTimerBlockHandler alloc] init];
    blockHandler.block = block;
    
    return [self timerWithTimeInterval:seconds target:blockHandler selector:@selector(ty_handleBlockWithTimer:) userInfo:nil repeats:inRepeats];
    
}

@end

@implementation tyTimerBlockHandler

@synthesize block;

- (void)ty_handleBlockWithTimer:(NSTimer *)timer
{
    HandleTimerBlock callbackBlock = self.block;
    
    NSAssert(callbackBlock != nil, @"Timer run block is nil");
    
    callbackBlock();
}

@end
